<?php namespace App\Http\Controllers;

use App\Models\Customer as MCustomer;
use App\Models\Goods;
use App\Models\Order;
use App\Models\ShopCar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Customer extends Controller
{
    public function index(Request $request)
    {
        $page = $request->input('page');
        $query = DB::table('customers')
            ->select('*');
        if ($filterText = $request->input('filter.text')) {
            $query->where(function ($query) use ($filterText) {
                $query->where('customers.username', 'like', '%' . $filterText . '%')
                    ->orWhere('customers.nickname', 'like', '%' . $filterText . '%')
                    ->orWhere('customers.phone', 'like', '%' . $filterText . '%')
                    ->orWhere('customers.email', 'like', '%' . $filterText . '%');
            });
        }
        return $query->paginate($page['size'], ['*'], 'page', $page['current']);
    }

    //获取用户
    public function show($id)
    {
        $customer = MCustomer::find($id);
        return response()->json($customer);
    }

    //添加用户
    public function store(Request $request)
    {
        // 表单验证，配合前台验证
        $this->validate($request, [
            'username' => 'unique:admins',
            'phone' => 'unique:admins',
            'email' => 'unique:admins',
            'password' => 'required'
        ]);
        $customer = new MCustomer;
        $customer->username = $request->input('username');
        $customer->phone = $request->input('phone');
        $customer->email = $request->input('email');
        $customer->nickname = $request->input('nickname');
        $customer->password = bcrypt($request->input('password'));
        $customer->address = $request->input('address');
        $customer->save();
        return response()->json($customer->id);
    }

    //修改用户
    public function update(Request $request, $id)
    {
        // 表单验证，配合前台验证
        $this->validate($request, [
            'username' => 'unique:admins,username,' . $id,
            'phone' => 'unique:admins,phone,' . $id,
            'email' => 'unique:admins,email,' . $id
        ]);

        $customer = MCustomer::find($id);
        assert($customer);
        $customer->username = $request->input('username');
        $customer->phone = $request->input('phone');
        $customer->email = $request->input('email');
        $customer->nickname = $request->input('nickname');
        $password = $request->input('password');
        if ($password != null)
            $customer->password = bcrypt($password);
        $customer->address = $request->input('address');
        $customer->save();
        return response('', 200);
    }

    //删除用户
    public function destroy($id)
    {
        $customer = MCustomer::find($id);
        if (!$customer) {
            return response('传参错误，未找到对象！', 500);
        }
        $customer->delete();
        return response('', 200);
    }

    // 获取未结算购物车商品
    public function shopCar()
    {
        $shop_car = DB::table('shop_car')->where('user_id', 2)
            ->where('status', 1)
            ->leftJoin('goods', 'goods.id', '=', 'shop_car.goods_id')
            ->select('goods.money as price', 'goods.image', 'goods.title', 'shop_car.*')
            ->get();
        return response()->json($shop_car);
    }

    // 结算购物车商品
    public function shop(Request $request)
    {
        $ids = $request->input('ids');
//        $user_id = Auth::user()->id;
        $user_id = 2;
        DB::transaction(function () use ($ids, $user_id) {
            foreach ($ids as $id) {
                $shop_car = ShopCar::find($id);
                $shop_car->status = 2;
                $shop_car->save();
                $order = new Order;
                $order->user_id = $user_id;
                $order->shop_car_id = $shop_car->id;
                $order->goods_id = $shop_car->goods_id;
                $order->save();
                $goods = Goods::find($shop_car->goods_id);
                $goods->number = $goods->number - $shop_car->number;
                $goods->save();
            }
        });
        return response('', 200);
    }

    // 查看我的订单
    public function order()
    {
        $orders = DB::table('orders')
            ->leftJoin('shop_car', function ($join) {
                $join->on('shop_car.id', '=', 'orders.shop_car_id')
                    ->where('shop_car.user_id', 2);
            })
            ->leftJoin('goods', 'goods.id', '=', 'orders.goods_id')
            ->select('orders.*', 'goods.money as price', 'goods.image', 'goods.title')
            ->where('orders.status', 1)
            ->get();
        return response()->json($orders);
    }
}
